package cn.rengy.tpa.wxmp.wxapp.message;

import com.alibaba.fastjson.JSONObject;

import cn.rengy.tpa.wxmp.message.template.TemplateMsgData;
import cn.rengy.tpa.wxmp.token.AccessToken;
import cn.rengy.tpa.wxmp.util.WeChatUtil;

/**
 * 组合模板并添加至帐号下的个人模板库
 * @author rengy
 *
 */
public class TemplateMessageApi {

	private final static String ADD_TEMPLATE_URL="https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token=";
	private final static String UNIFORM_MESSAGE_URL="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=";
	private final static String EMPHASIS_KEYWORD_POSTFIX=".DATA";
	private static final String TEMPLATE_MESSAGE_URL="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=";
	private static final String DELETE_TEMPLATE_URL="https://api.weixin.qq.com/cgi-bin/wxopen/template/del?access_token=";
	private static final String GET_TEMPLATE_LIBRARY_URL="https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get?access_token=";
	/**
	 * 添加模板
	 * @param accessToken
	 * @param addTemplateRequest
	 * @return
	 */
	public static String addTemplate(AccessToken accessToken,AddTemplateRequest addTemplateRequest) {
		String post=JSONObject.toJSONString(addTemplateRequest);
		JSONObject resultObject=WeChatUtil.postSuccess(ADD_TEMPLATE_URL, post, accessToken);
		return resultObject.getString("template_id");
	}
	/**
	 * 删除模板
	 * @param accessToken
	 * @param template_id
	 */
	public static void deleteTemplate(AccessToken accessToken,String template_id) {
		JSONObject postObject = new JSONObject();
		postObject.put("template_id", template_id);
		WeChatUtil.postSuccess(DELETE_TEMPLATE_URL, postObject.toJSONString(), accessToken);
	}
	/**
	 * 获取模板库某个模板标题下关键词库
	 * @param accessToken
	 * @param id
	 * @return
	 */
	public static TemplateResponse getTemplateLibraryById(AccessToken accessToken,String id) {
		JSONObject postObject = new JSONObject();
		postObject.put("id", id);
		JSONObject resultObject=WeChatUtil.postSuccess(GET_TEMPLATE_LIBRARY_URL, postObject.toJSONString(), accessToken);
		return null;
	}
	
	private static final String TEMPLATE_LIBRARY_LIST_URL="https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list?access_token=";
	/**
	 * 获取小程序模板库标题列表
	 */
	public static Object getTemplateLibraryList(AccessToken accessToken,int offset,int count) {
		JSONObject postObject = new JSONObject();
		postObject.put("offset", offset);
		postObject.put("count", count);
		JSONObject resultObject=WeChatUtil.postSuccess(TEMPLATE_LIBRARY_LIST_URL, postObject.toJSONString(), accessToken);
		return null;
	}
	private static final String TEMPLATE__LIST_URL="https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=";
	/**
	 * 获取帐号下已存在的模板列表
	 * @param accessToken
	 * @param offset
	 * @param count
	 * @return
	 */
	public static Object getTemplateList(AccessToken accessToken,int offset,int count) {
		JSONObject postObject = new JSONObject();
		postObject.put("offset", offset);
		postObject.put("count", count);
		JSONObject resultObject=WeChatUtil.postSuccess(TEMPLATE__LIST_URL, postObject.toJSONString(), accessToken);
		return null;
	}
	/**
	 * 统一服务消息
	 * @param accessToken
	 * @param uniformMessageRequest
	 */
	public static void sendUniformMessage(AccessToken accessToken,UniformMessageRequest uniformMessageRequest) {
		JSONObject request = new JSONObject();
		request.put("touser", uniformMessageRequest.getTouser());
		WeappTemplateMsg weappTemplateMsg=uniformMessageRequest.getWeappTemplateMsg();
		MpTemplateMsg mpTemplateMsg=uniformMessageRequest.getMpTemplateMsg();
		JSONObject template_msg=new JSONObject();
		if(weappTemplateMsg!=null) {//小程序模板
			template_msg.put("template_id", weappTemplateMsg.getTemplate_id());
			template_msg.put("page", weappTemplateMsg.getPage());
			template_msg.put("form_id", weappTemplateMsg.getForm_id());
			JSONObject msgdata = new JSONObject();
			for(WeappTemplateMsgData data:weappTemplateMsg.getData()){
				JSONObject dataKey = new JSONObject();
				dataKey.put("value",data.getValue());
				msgdata.put(data.getName(), dataKey);
				if(data.isEmphasis()){
					template_msg.put("emphasis_keyword", data.getName().concat(EMPHASIS_KEYWORD_POSTFIX));
				}
			}
			template_msg.put("data", msgdata);
			request.put("weapp_template_msg", template_msg);
		}else if(mpTemplateMsg!=null) {//公众号模板
			template_msg.put("appid", mpTemplateMsg.getAppid());
			template_msg.put("template_id", mpTemplateMsg.getTemplate_id());
			template_msg.put("url", mpTemplateMsg.getUrl());
			JSONObject miniprogramdata = new JSONObject();
			miniprogramdata.put("appid", mpTemplateMsg.getMiniprogram().getAppid());
			miniprogramdata.put("pagepath", mpTemplateMsg.getMiniprogram().getPagepath());
			template_msg.put("miniprogram", miniprogramdata);
			JSONObject msgdata = new JSONObject();
			for(TemplateMsgData data:mpTemplateMsg.getData()) {
				JSONObject dataKey = new JSONObject();
				dataKey.put("value", data.getValue());
				dataKey.put("color", data.getColor());
				msgdata.put(data.getName(), dataKey);
			}
			template_msg.put("data", msgdata);
			request.put("mp_template_msg", template_msg);
		}else {
			throw new IllegalArgumentException("模板信息为空");
		}
		String post = request.toJSONString();
		WeChatUtil.postSuccess(UNIFORM_MESSAGE_URL, post, accessToken);
	}
	
	/**
	 * 发送模板消息
	 * @param accessToken
	 * @param templateMessageRequest
	 */
	public static void sendTemplateMessage(AccessToken accessToken,TemplateMessageRequest templateMessageRequest) {
		JSONObject request = new JSONObject();
		request.put("touser", templateMessageRequest.getTouser());
		request.put("template_id", templateMessageRequest.getTemplate_id());
		request.put("page", templateMessageRequest.getPage());
		request.put("form_id", templateMessageRequest.getForm_id());
		JSONObject msgdata = new JSONObject();
		for(WeappTemplateMsgData data:templateMessageRequest.getData()){
			JSONObject dataKey = new JSONObject();
			dataKey.put("value",data.getValue());
			msgdata.put(data.getName(), dataKey);
			if(data.isEmphasis()){
				request.put("emphasis_keyword", data.getName().concat(EMPHASIS_KEYWORD_POSTFIX));
			}
		}
		request.put("data", msgdata);
		String post = request.toJSONString();
		WeChatUtil.postSuccess(TEMPLATE_MESSAGE_URL, post, accessToken);
	}
}
